package com.jushu.storbox.network;

import java.util.Set;

import android.app.ActionBar.LayoutParams;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.jushu.storbox.R;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.manager.DownloadTaskManager;
import com.jushu.storbox.manager.UploadTaskManager;
import com.jushu.storbox.task.tqueue.DownloadTask;
import com.jushu.storbox.task.tqueue.UploadTask;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.view.dialog.ClouddriveDialog;

public class NetworkReceiver extends BroadcastReceiver {

	private long lastMills = 0;

	@SuppressWarnings({})
	@Override
	public void onReceive(Context context, Intent intent) {
		WifiController wifiController = new WifiController(context);
		int status = wifiController.getNetWord();
		if (status == 1 || status == 2) {
			if (ClientConfig.NETWORK_ISWIFI
					.equals(wifiController.getWifiType())
					|| !PublicTools.getDownloadRemind(context)) {
				doRetryTask();
			} else {
				final ClouddriveDialog menuDialog = new ClouddriveDialog(
						context, R.style.myDialogTheme,
						R.layout.window_wifi_dialog);
				menuDialog.setHeigth(LayoutParams.MATCH_PARENT);
				menuDialog.setWidth(LayoutParams.MATCH_PARENT);
				menuDialog.setCanceledOnTouchOutside(true);
				menuDialog.show();
				View conventView = menuDialog.getConventView();
				Button confirmBt = (Button) conventView
						.findViewById(R.id.confirm_btn);

				confirmBt.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						menuDialog.dismiss();
						doRetryTask();
					}
				});
				Button cancleBt = (Button) conventView
						.findViewById(R.id.cancle_btn);
				cancleBt.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						menuDialog.dismiss();
					}
				});
			}
		} else if (status == 0) {
			if (lastMills == 0) {
				Toast.makeText(context,
						context.getString(R.string.allfile_offline_status),
						Toast.LENGTH_SHORT).show();
				lastMills = System.currentTimeMillis();
			} else {
				if (System.currentTimeMillis() - lastMills > 3000) {
					Toast.makeText(context,
							context.getString(R.string.allfile_offline_status),
							Toast.LENGTH_SHORT).show();
					lastMills = System.currentTimeMillis();
				}
			}
		}
	}

	private void doRetryTask() {
		// List<DownloadObject> listdown = DAOFactory
		// .instance(context).getDownloadFileDao().getDownloadFileList();
		// for(DownloadObject dObject:listdown){
		// DownloadTask dTask=new DownloadTask(context, 0, null, dObject,
		// dObject.isSync());
		// DownloadTaskManager.addTask(dTask);
		// }
		Set<DownloadTask> failTasks = DownloadTaskManager.getAllFailTasks();
		if (failTasks != null && failTasks.size() > 0) {
			for (DownloadTask task : failTasks) {
				DownloadTaskManager.removeFailTask(task);
				DownloadTaskManager.addTask(task);
			}
		}
		// cancel upload task
		// List<UploadObject> listup =
		// DAOFactory.instance(context).getUploadFileDao().getUploadFileList();
		// for(UploadObject uObject:listup){
		// UploadTask uTask=new UploadTask(context, uObject.getParent(),
		// uObject.getLoctPath(),null);
		// UploadTaskManager.addTask(uTask);
		// }
		Set<UploadTask> uploadTasks = UploadTaskManager.getAllFailTaskes();
		if (uploadTasks != null && uploadTasks.size() > 0) {
			for (UploadTask task : uploadTasks) {
				UploadTaskManager.removeFailTask(task);
				UploadTaskManager.addTask(task);
			}
		}
	}
}
